package com.seal.yoyoshop.adminweb;

import com.seal.yoyoshop.bin.Admin;
import com.seal.yoyoshop.service.AdminService;
import com.seal.yoyoshop.service.impl.AdminServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminLoginServlet", value = "/admin/login")
public class AdminLoginServlet extends HttpServlet {

    private AdminService adminService = new AdminServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Admin admin = adminService.getAdminByUsernameAndPassword(username,password);
        if(admin == null){
            request.setAttribute("msg","登陆失败，用户名或密码错误");

            request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        }else {

            //获取session
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);
            //跳转到首页
            request.getRequestDispatcher("/admin/index.jsp").forward(request,response);
        }


    }
}
